﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StudentManage.Data;

namespace StudentManage.Web.Personal
{
    public partial class record : SecurityPage
    {
        private void RepApplyBing()
        {
            UserList md = (UserList)Session["UserMD"];
            string strtitle = (this.txt_Search.Text.Trim() == "请输入查询的标题") ? "" : this.txt_Search.Text.Trim();
            var eps = DynamicLinqExpressions.True<Apply>();
            eps = eps.And(p => p.ProjectTitle.Contains(strtitle));
            eps = eps.And(p => p.Userid == md.id);
            eps = eps.And(p => p.IsDel == false);
            if (this.txt_DateStart.Text.Trim() != "")
            {
                DateTime StartTime = Convert.ToDateTime(this.txt_DateStart.Text.Trim());
                eps = eps.And(p => p.ApplyTime >= StartTime);
            }
            if (this.txt_DateEnd.Text.Trim() != "")
            {
                DateTime EndTime = Convert.ToDateTime(this.txt_DateEnd.Text.Trim());
                eps = eps.And(p => p.ApplyTime <= EndTime);
            }

            int RowCount = LinqUtility.LinqContext.Apply.Where(eps).Count();
            this.AspNetPager1.RecordCount = RowCount;
            int PageSize = this.AspNetPager1.PageSize;
            int SkipCount = PageSize * (this.AspNetPager1.CurrentPageIndex - 1);

            List<Apply> Applylist = LinqUtility.LinqContext.Apply.Where(eps).OrderByDescending(p => p.AddTime).Skip(SkipCount).Take(PageSize).ToList();
            this.RepApplylist.DataSource = Applylist;
            this.RepApplylist.DataBind();
        }

        public string GetProjectType(string Pid)
        {
            Project md = Project.Where("id=@0", int.Parse(Pid)).First();
            string TypeName = Enum.GetName(typeof(EnumData.ProjectType), Convert.ToInt32(md.ProjectTypeid));
            string TypeInfoName = TypeInfo.Where("id=@0", md.Typeid).First().TypeName;
            return TypeName + "-" + TypeInfoName;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            if (Session["UserMD"] != null)
                this.RepApplyBing();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            this.RepApplyBing();
        }

        protected void Btn_Search_Click(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            this.RepApplyBing();
        }

        protected void RepApplylist_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int Applyid = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "UnApply")
            {
                Apply md = Apply.Where("id=@0", Applyid).First();
                if (Convert.ToBoolean(md.IsAudit))
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('已审批通过，不能取消！')</script>");
                    return;
                }
                md.IsDel = true;
                Apply.Update(md, "id=@0", md.id);
            }
            this.RepApplyBing();
        }
    }
}